const router = require('koa-router')();
const cc = require('../conCtl');
const js = require('../jsCtl');
const db = require('../dbCtl');
const md5 = require('md5-node');

/**
@hostpath = http://127.0.0.1:11102/service/admin
 */

/** 修改密码**************************************************************************/
router.put('/', async c => {
  js.check_params(
    !c.request.body.id && '!用户ID' ||
    !c.request.body.account && '!账号' ||
    !c.request.body.pwd && '!密码' ||
    !c.request.body.newpwd && '!新密码');

  let pwd = await db.model('`t_admin`').where('`id`=?', c.request.body.id).shift('没有找到用户', 'pwd');

  if (md5(c.request.body.pwd) != pwd) js.throwObj('旧密码错误', js.rescode.param_fail);

  await db.model('`t_admin`')
    .where('`id`=?', c.request.body.id)
    .relyUpdate('修改失败',{
      account: c.request.body.account,
      pwd: md5(c.request.body.newpwd)
    });
  cc.send(c);
});


module.exports = router;